import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {CheckstandPage} from "../checkstand/checkstand";
import {ROUTER} from "../../service/router.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {GoodModel} from "../../model/good.model";
import {APARTMENTAPISERVICE} from "../../api/apartment-api.service";
import {GOODS} from "../../api/goods-api";
import {CheckStandModel} from "../../model/checkstand.model";

/**
 * Generated class for the RentStoreOrderPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-rent-store-order',
  templateUrl: 'rent-store-order.html',
})
export class RentStoreOrderPage {

  good:GoodModel;
  data={
    layoutId:undefined,
    rentDate:1,
    time:1
  };

  monthItems = [
    {name: '1月', value: 1,time:1},
    {name: '3月', value: 2,time:3},
    {name: '6月', value: 3,time:6},
    {name: '1年', value: 4,time:12},
    {name: '2年', value: 5,time:24}
  ];

  layoutList=[];

  moneyItems=[
    {
      name:'单人沙发',
      item:'100元/月'
    },
    {
      name:'合计',
      item:'100.00元'
    }
  ];

  constructor(public GOODS:GOODS,private apartmentApi:APARTMENTAPISERVICE,public ls:LocalStorage,public navCtrl: NavController, public navParams: NavParams,private ROUTER: ROUTER) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RentStoreOrderPage');
    this.good = this.ls.getObject('good') as GoodModel;
    this.moneyItems[0].name = this.good.title;
    this.moneyItems[0].item = this.moneyItems[1].item = this.good.salesPrice;
    console.log(this.moneyItems);
    this.loadMyHouse();
  }
  loadMyHouse() {
    this.apartmentApi.getMyRentingHouse({}, (res:any)=> {
      this.layoutList = res;
      if(this.layoutList.length > 0){
        this.data.layoutId = this.layoutList[0].id
      }
    })
  }
  getTimeByValue(value):number{
    for(let v of this.monthItems){
      if (v.value == value){
        return v.time;
      }
    }
  }
  gotoCheckstand() {
    let reqData = {
      totalMoney:this.good.salesPrice*this.getTimeByValue(this.data.rentDate),
      houseId: this.data.layoutId,
      goodsId: this.good.id,
      source: 1,
      lease:this.data.rentDate
    }
    this.GOODS.purchaseRent(reqData, (v)=> {
      console.log(v);
      let checkstand = new CheckStandModel(v.orderNo, v.title, v.totalMoney, v.id,ROUTER.MYRENT)
      this.ls.setObject('checkstand', checkstand);
      this.ROUTER.go(ROUTER.CHECK);
    })

  }


}
